import { getToken, removeToken, setToken } from '@/utils/auth';
// import request from '@/utils/request'
import { login, getCodeImg } from '@/api/login';
import { getMenu } from '@/api/menu';
import { getDeptList } from '@/api/user';

export default {
	id: 'user',
	state: () => {
		return {
			test: '',
			token: '231',
			roles: [],
			permissions: [],
			loginUser: null,
			menuList: [],
			deptList: [],
			nodeInfo: undefined,
		};
	},
	getters: {
		getRoles: (state) => state.roles,
		getTokenInfo: (state) => {
			if (state.token) {
				return state.token;
			} else {
				return getToken();
			}
		},
		getLoginUser: (state) => state.loginUser,
		getPermissions: (state) => state.permissions,
	},
	actions: {
		// 登录
		async login(payload) {
			return await login(payload);
		},
		async getCaptcha(payload) {
			return await getCodeImg(payload);
		},
		async getMenuList() {
			const res = await getMenu();
			this.menuList = res.page;
			return res;
		},
		async getDeptList(payload) {
			return await getDeptList(payload);
		},
		setSelectNode(payload) {
			this.nodeInfo = payload;
		},
		setToken(payload) {
			this.token = payload;
			setToken(payload);
		},
		removeToken() {
			this.token = '';
			removeToken();
		},
		setRoles(roles) {
			this.roles = roles;
		},
		setPermissions(permissions) {
			this.permissions = permissions;
		},
		setLoginUser(loginUser) {
			this.loginUser = loginUser;
		},
	},
	// 开启数据缓存
	persist: {
		enabled: true,
		strategies: [
			{
				key: 'userInfo',
				storage: localStorage,
				// 存储 state 中的属性信息
				paths: ['token', 'roles', 'permissions', 'loginUser', 'menuList'],
			},
		],
	},
};
